/**
 * 地图弹框详情数据过滤
 *
 * @author 阳坤（1194636148@qq.com）
 * @date   时间：2022/5/26
 */

import dayjs from 'dayjs'

/**
 * 处理弹框内容信息
 *
 * currentData     数据项
 * currentType     数据详情
 */
export const filterMapDialogDetail = (currentData, currentType) => {
  // 道路事件详情
  if (currentType == 'dlsj') return getControlDetail(currentData)
  // 疫情防控详情
  if (currentType == 'yqfk') return getEpidemicDetail(currentData)
}

/**
 * 得到道路事件详情
 *
 */
const getControlDetail = data => {
  let controObj = null
  const trafficOutArr = []
  const trafficIntoArr = []
  data[0].forEach(item => {
    // 找到对应的收费站  找不到默认开启
    controObj = data[1].find(val => item.tollstationid == val.tollstationid)
    // infotype   0开启 1关闭
    if (controObj) {
      item.infotype = controObj.infotype
      item.closereason = controObj.closereason
      item.accidentdesc = controObj.event && controObj.event.accidentdesc
    } else item.infotype = '0'

    // io   0出口 1入口
    if (item.io == 0) trafficOutArr.push(item)
    else if (item.io == 1) trafficIntoArr.push(item)
  })

  // 得到交通管制原因
  let accidentValue = '' // 交通管制对象
  let intoValue = '' // 入口是否关闭
  let outValue = '' // 出口是否关闭
  let switchValue = '' // 显示开关状态
  let closereasonText = '' // 交通管制原因
  const closereasonArr = ['恶劣天气', '交通事故', '警卫任务', '诱导分流', '养护施工', '其他']
  intoValue = trafficIntoArr.some(val => val.infotype == 1)
  outValue = trafficOutArr.some(val => val.infotype == 1)
  switchValue = outValue ? (intoValue ? '，出口、入口关闭' : '，出口关闭') : intoValue ? '，入口关闭' : ''
  // 先在入口里找交通管制原因
  accidentValue = trafficIntoArr.find(val => val.closereason)
  if (accidentValue) closereasonText = '因' + closereasonArr[accidentValue.closereason] + '原因' + switchValue
  else {
    // 否则在入口里找交通管制原因
    accidentValue = trafficOutArr.find(val => val.closereason)
    if (accidentValue) closereasonText = '因' + closereasonArr[accidentValue.closereason] + '原因' + switchValue
    else closereasonText = ''
  }
  return {
    trafficOutArr,
    trafficIntoArr,
    closereasonText
  }
}

/**
 * 得到疫情防控详情
 *
 */
const getEpidemicDetail = data => {
  const roadNum = { title: '路线编号：', value: '/' }
  const roadName = { title: '路线名称：', value: '/' }
  const pile = { title: '桩号：', value: '/' }
  const principal = { title: '负责人姓名：', value: '/' }
  const phone = { title: '联系人电话：', value: '/' }
  const stakeMarkKm = data.stakeMarkKm ? 'K' + data.stakeMarkKm : '/'
  const stakeMarkM = data.stakeMarkM ? '+' + data.stakeMarkM : ''
  if (data.roadNum) roadNum.value = data.roadNum
  if (data.roadName) roadName.value = data.roadName
  pile.value = `${stakeMarkKm}${stakeMarkM}`
  if (data.principal) principal.value = data.principal
  if (data.phone) phone.value = data.phone
  return [roadNum, roadName, pile, principal, phone]
}

/**
 * 数据判空
 * @param data       数据对象
 * @param key        对象的key
 * @param emptyBack  为空返回值
 */
export const filterEmpty = (object, key, emptyBack = '') => {
  if (!object || object.constructor !== Object) return emptyBack
  let value = emptyBack
  try {
    for (const k in object) {
      if (k == key) {
        value = object[key] || emptyBack
        break
      }
      if (object[k] && object[k].constructor === Object) {
        value = getObjVal(object[k], key, emptyBack)
        break
      }
    }
  } catch (error) {
    console.log(error)
  }

  return value
}
